package com.liu.xml;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class CreateDocument1 {
	public static void main(String[] args) throws Exception {
		generateXmlCfg();
	}

	public static void generateXmlCfg() throws Exception {

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document doc = builder.newDocument();

		Element root = doc.createElement("PerformanceMonitor");
		doc.appendChild(root);
		root.appendChild(doc.createTextNode("\n\t"));

		Element id = doc.createElement("id");
		id.appendChild(doc.createTextNode("id"));
		root.appendChild(id);
		root.appendChild(doc.createTextNode("\n\t"));

		Element deviceID = doc.createElement("deviceId");
		deviceID.appendChild(doc.createTextNode("deviceId"));
		root.appendChild(deviceID);
		root.appendChild(doc.createTextNode("\n\t"));

		Element cardId = doc.createElement("cardId");
		cardId.appendChild(doc.createTextNode("cardId"));
		root.appendChild(cardId);
		root.appendChild(doc.createTextNode("\n\t"));

		Element iFId = doc.createElement("iFId");
		iFId.appendChild(doc.createTextNode("iFId"));
		root.appendChild(iFId);
		root.appendChild(doc.createTextNode("\n\t"));

		Element linkId = doc.createElement("linkId");
		linkId.appendChild(doc.createTextNode("linkId"));
		root.appendChild(linkId);
		root.appendChild(doc.createTextNode("\n\t"));

		Element time = doc.createElement("time");
		time.appendChild(doc.createTextNode("time"));
		root.appendChild(time);
		root.appendChild(doc.createTextNode("\n\t"));

		Element type = doc.createElement("type");
		type.appendChild(doc.createTextNode("type"));
		root.appendChild(type);
		root.appendChild(doc.createTextNode("\n\t"));

		Element typeOutput = doc.createElement("typeOutput");
		typeOutput.appendChild(doc.createTextNode("typeOutput"));
		root.appendChild(typeOutput);
		root.appendChild(doc.createTextNode("\n\t"));

		Element appType = doc.createElement("appType");
		appType.appendChild(doc.createTextNode("appType"));
		root.appendChild(appType);
		root.appendChild(doc.createTextNode("\n\t"));

		Element appTypeOutput = doc.createElement("appTypeOutput");
		appTypeOutput.appendChild(doc.createTextNode("appTypeOutput"));
		root.appendChild(appTypeOutput);
		root.appendChild(doc.createTextNode("\n\t"));

		Element transType = doc.createElement("transType");
		transType.appendChild(doc.createTextNode("transType"));
		root.appendChild(transType);
		root.appendChild(doc.createTextNode("\n\t"));

		Element transTypeOutput = doc.createElement("transTypeOutput");
		transTypeOutput.appendChild(doc.createTextNode("transTypeOutput"));
		root.appendChild(transTypeOutput);
		root.appendChild(doc.createTextNode("\n\t"));

		Element netType = doc.createElement("netType");
		netType.appendChild(doc.createTextNode("netType"));
		root.appendChild(netType);
		root.appendChild(doc.createTextNode("\n\t"));

		Element netTypeOutput = doc.createElement("netTypeOutput");
		netTypeOutput.appendChild(doc.createTextNode("netTypeOutput"));
		root.appendChild(netTypeOutput);
		root.appendChild(doc.createTextNode("\n\t"));

		Element linkType = doc.createElement("linkType");
		linkType.appendChild(doc.createTextNode("linkType"));
		root.appendChild(linkType);
		root.appendChild(doc.createTextNode("\n\t"));

		Element linkTypeOutput = doc.createElement("linkTypeOutput");
		linkTypeOutput.appendChild(doc.createTextNode("linkTypeOutput"));
		root.appendChild(linkTypeOutput);
		root.appendChild(doc.createTextNode("\n\t"));

		Element filterSummary = doc.createElement("filterSummary");
		root.appendChild(filterSummary);

		filterSummary.appendChild(doc.createTextNode("\n\t\t"));
		Element _filter = doc.createElement("filter");
		_filter.appendChild(doc.createTextNode("\n\t\t\t"));
		Element filterType = doc.createElement("filterType");
		filterType.appendChild(doc.createTextNode("filterType"));
		_filter.appendChild(filterType);

		_filter.appendChild(doc.createTextNode("\n\t\t\t"));
		Element filterInfo = doc.createElement("filterInfo");
		filterInfo.appendChild(doc.createTextNode("filterInfo"));
		_filter.appendChild(filterInfo);

		_filter.appendChild(doc.createTextNode("\n\t\t\t"));

		Element _filterData = doc.createElement("filterData");

		_filterData.appendChild(doc.createTextNode("\n\t\t\t\t"));
		Element filterLayerType = doc.createElement("filterLayerType");
		filterLayerType.appendChild(doc.createTextNode("filterLayerType"));
		_filterData.appendChild(filterLayerType);

		_filterData.appendChild(doc.createTextNode("\n\t\t\t\t"));
		Element filterValue = doc.createElement("filterValue");
		filterValue.appendChild(doc.createTextNode("filterValue"));
		_filterData.appendChild(filterValue);
		_filter.appendChild(_filterData);

		filterSummary.appendChild(_filter);
		root.appendChild(doc.createTextNode("\n\t"));

		Element userNum = doc.createElement("userNum");
		userNum.appendChild(doc.createTextNode("userNum"));
		root.appendChild(userNum);

		root.appendChild(doc.createTextNode("\n\t"));

		Element ip = doc.createElement("ip");
		ip.appendChild(doc.createTextNode("\n\t\t"));
		Element ip1 = doc.createElement("ip1");
		ip1.appendChild(doc.createTextNode("ip1"));
		ip.appendChild(ip1);

		ip.appendChild(doc.createTextNode("\n\t\t"));
		Element ip1Prefix = doc.createElement("ip1Prefix");
		ip1Prefix.appendChild(doc.createTextNode("ip1Prefix"));
		ip.appendChild(ip1Prefix);

		ip.appendChild(doc.createTextNode("\n\t\t"));
		Element ip2 = doc.createElement("ip2");
		ip2.appendChild(doc.createTextNode("ip2"));
		ip.appendChild(ip2);

		ip.appendChild(doc.createTextNode("\n\t\t"));
		Element ip2Prefix = doc.createElement("ip2Prefix");
		ip2Prefix.appendChild(doc.createTextNode("ip2Prefix"));
		ip.appendChild(ip2Prefix);
		ip.appendChild(doc.createTextNode("\n\t"));
		root.appendChild(ip);

		root.appendChild(doc.createTextNode("\n\t"));
		Element userGroup = doc.createElement("userGroup");
		userGroup.appendChild(doc.createTextNode("userGroup"));
		root.appendChild(userGroup);
		root.appendChild(doc.createTextNode("\n\t"));
		Element regionName = doc.createElement("regionName");
		regionName.appendChild(doc.createTextNode("regionName"));
		root.appendChild(regionName);

		root.appendChild(doc.createTextNode("\n\t"));
		Element period = doc.createElement("period");
		period.appendChild(doc.createTextNode("period"));
		root.appendChild(period);
		root.appendChild(doc.createTextNode("\n\t"));
		Element deadlineTime = doc.createElement("deadlineTime");
		deadlineTime.appendChild(doc.createTextNode("deadlineTime"));
		root.appendChild(deadlineTime);

		/*-----------server节点开始-----------*/
		for (int i = 0; i <= 4; i++) {
			root.appendChild(doc.createTextNode("\n\t"));
			Element server = doc.createElement("server");
			root.appendChild(server);
			server.appendChild(doc.createTextNode("\n\t\t"));
			
			/**
			 * serverID节点 
			 */
			Element serverID = doc.createElement("serverID");
			serverID.appendChild(doc.createTextNode("11"));
			server.appendChild(serverID);
			server.appendChild(doc.createTextNode("\n\t\t"));

			/**
			 * serverName节点 
			 */
			Element serverName = doc.createElement("serverName");
			serverName.appendChild(doc.createTextNode("OA系統"));
			server.appendChild(serverName);
			server.appendChild(doc.createTextNode("\n\t\t"));

			/**
			 * ip_port节点 
			 * 当start_ip为0 和 end_ip为0xffffffff时代表匹配任意IP，即只匹配端口。
			 * 当start_Port 为0 和end_Port 为65535时代表匹配任意端口，相当于IP段不带端口。
			 */
			Element ip_port = doc.createElement("ip_port");
			ip_port.appendChild(doc.createTextNode("\n\t\t\t"));
			
			Element s_ip = doc.createElement("ip");
			s_ip.appendChild(doc.createTextNode("192.168.2.236"));
			ip_port.appendChild(s_ip);
			ip_port.appendChild(doc.createTextNode("\n\t\t\t"));
			
			Element mask = doc.createElement("mask");
			mask.appendChild(doc.createTextNode("32"));
			ip_port.appendChild(mask);
			
			ip_port.appendChild(doc.createTextNode("\n\t\t\t"));
			Element start_port = doc.createElement("start_port");
			start_port.appendChild(doc.createTextNode("1"));
			ip_port.appendChild(start_port);
			
			ip_port.appendChild(doc.createTextNode("\n\t\t\t"));
			Element end_port = doc.createElement("end_port");
			end_port.appendChild(doc.createTextNode("8080"));
			ip_port.appendChild(end_port);
			
			ip_port.appendChild(doc.createTextNode("\n\t\t"));
			server.appendChild(ip_port);
			
			/**
			 * dn节点 
			 */
			server.appendChild(doc.createTextNode("\n\t\t"));
			
			Element dn = doc.createElement("dn");

			dn.appendChild(doc.createTextNode("\n\t\t\t"));
			Element name = doc.createElement("name");
			name.appendChild(doc.createTextNode("baidu.com"));
			dn.appendChild(name);
			
			dn.appendChild(doc.createTextNode("\n\t\t"));
			server.appendChild(dn);
			server.appendChild(doc.createTextNode("\n\t"));
		}
		/*-----------server结束-----------*/
		File xmlFile = new File("C:\\Users\\12961\\Desktop\\person.xml");
		if (!xmlFile.getParentFile().exists()) {// 判断文件夹是否存在
			xmlFile.getParentFile().mkdirs();
		}

		OutputStream fos = new FileOutputStream(xmlFile);
		OutputStreamWriter outwriter = new OutputStreamWriter(fos);

		Source source = new DOMSource(doc);
		Result result = new StreamResult(outwriter);
		Transformer xformer = TransformerFactory.newInstance().newTransformer();
		xformer.setOutputProperty(OutputKeys.INDENT, "yes");
		xformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
		xformer.transform(source, result);

		fos.close();
		System.out.println("写xml成功");
	}

}
